package com.api.gbhg.liuwenbo.version3.api;

import com.alibaba.fastjson.JSON;
import com.api.gbhg.liuwenbo.version2.api.BudgetSectionIncomeApi2;
import com.api.gbhg.liuwenbo.version2.pojo.BudgetSectionIncome2;
import com.api.gbhg.liuwenbo.version3.util.ApiUtil;

import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

/**
 * @Description 各独立预算单位收支预算明细表
 * @Version 3.0
 * @Author lwb
 * @Date 22.12.26
 */
@Path("v3/BudgetSectionIncome")
public class BudgetSectionIncomeApi3 {

    private BudgetSectionIncomeApi2 budapi2 = new BudgetSectionIncomeApi2();

    @POST
    @Path("/getBudgetIncomeList")
    @Produces(MediaType.APPLICATION_JSON)
    public String getBudgetIncomeList(Map<String,Object> param){
        Map msgMap = new HashMap<>();

        if (param == null ||!param.containsKey("nf") || !param.containsKey("dw") ||!param.containsKey("jz")){
            msgMap.put("code","500");
            msgMap.put("msg","缺少参数 请检查参数是否完整！");
            msgMap.put("data","");
            return JSON.toJSONString(msgMap);
        }

        Map<String, Object> paramFormat = budapi2.paramFormat(param);

        List<BudgetSectionIncome2> czbksrData = null;
        List<BudgetSectionIncome2> fczbksrData = null;
        try {
            czbksrData = budapi2.getCzbksrData(paramFormat);
            fczbksrData = budapi2.getFczbksrData(paramFormat);


            Map<String,Object> map = new HashMap<>();
            map.put("财政拨款收入", ApiUtil.structureConversion(czbksrData,paramFormat.get("nf").toString()));
            map.put("非财政拨款收入",structureConversion(fczbksrData,paramFormat.get("nf").toString()));
            msgMap.put("code","200");
            msgMap.put("msg","");
            msgMap.put("data",map);


        }catch (Exception e){
            e.printStackTrace();
            msgMap.put("code","500");
            msgMap.put("msg","程序异常，请联系管理员！"+e.getMessage());
            msgMap.put("data","");
            return JSON.toJSONString(msgMap);
        }

        return JSON.toJSONString(msgMap);
    }

    /**
     * 结构变更
     * @param paramList
     *          数据集合
     * @param nf
     *          年份
     * @return
     */
    public static List<BudgetSectionIncome2> structureConversion(List<BudgetSectionIncome2> paramList, String nf){
        List<BudgetSectionIncome2> list = new LinkedList<>();

        for (int i=0;i<paramList.size();i++){
            BudgetSectionIncome2 bean = paramList.get(i);
            bean.setNf(nf);
            List<BudgetSectionIncome2> xjBean = bean.getXj();
            if (xjBean != null && xjBean.size()>0){
                for (int j = 0; j < xjBean.size(); j++) {
                    BudgetSectionIncome2 tempBean = xjBean.get(j);
                    xjChange(tempBean,bean,nf,list);
                }
            }
        }
        return list;
    }

    /**
     * 下级结构转换
     * @param xjBean
     *          下级bean
     * @param bean
     *          收入分类bean
     * @param nf
     *          年份
     * @param list
     *          目标集合
     */
    public static void xjChange(BudgetSectionIncome2 xjBean,BudgetSectionIncome2 bean,String nf,List<BudgetSectionIncome2> list){
        xjBean.setDwId(bean.getDwId());
        xjBean.setDwName(bean.getDwName());
        xjBean.setNf(nf);

        String dataid = xjBean.getId();
        xjBean.setId(bean.getId());
        xjBean.setName(bean.getName());
        xjBean.setDataid(dataid);

        list.add(xjBean);
    }

}
